﻿using System;
using System.Windows.Forms;
using Sedge.Core;
using Sedge.Core.Globalization;
using Sedge.UI.ViewModel;

namespace Sedge.UI.View
{
	public partial class CollectView : UserControl
	{
		private readonly CollectViewModel _collectViewModel;
		
		public CollectView()
		{
			InitializeComponent();
		}

		public CollectView(CollectViewModel collectViewModel)
			: this()
		{
			_collectViewModel = collectViewModel;
			_collectViewModel.ProgressEvent += CollectViewModel_ProgressEvent;
			SetupWindow();
		}

		private void SetupWindow()
		{
			labelHeader.Text = Locale.Strings.CollectHeaderGenerating;
			checkBoxDelete.Text = Locale.Strings.CollectDeleteReport;
			buttonSend.Text = Locale.Strings.CollectSendReport;
			buttonView.Text = Locale.Strings.CollectViewReport;

			int width = Math.Max(75, Math.Max(buttonSend.Width, buttonView.Width));
			buttonSend.Width = width;
			buttonView.Width = width;
			buttonSend.Left = progressBar.Right - buttonSend.Width;
			buttonView.Left = buttonSend.Left - buttonView.Width - 10;

			labelNotes.Text = _collectViewModel.Notes;
			checkBoxDelete.Checked = Services.ViewManager.CleanOnExit;

			progressBar.Minimum = 0;
			progressBar.Step = 1;
			progressBar.Maximum = _collectViewModel.StepsCount;
		}

		void CollectViewModel_ProgressEvent(int step, string state)
		{
			if (step < progressBar.Maximum)
			{
				progressBar.PerformStep();
				DisableControls(true);
			}
			else
			{
				progressBar.Value = progressBar.Maximum;
				labelHeader.Text = Locale.Strings.CollectHeaderDone;
				DisableControls(false);
			}
			labelHeader.Text = state;
		}

		private void DisableControls(bool disable)
		{
			if (buttonView.Enabled != disable)
				return;
			buttonView.Enabled = !disable;
			buttonSend.Enabled = !disable && _collectViewModel.CanSendReport();
			checkBoxDelete.Enabled = !disable;
		}

		private void buttonView_Click(object sender, EventArgs e)
		{
			_collectViewModel.ViewReport();
		}

		private void checkBoxDelete_CheckedChanged(object sender, EventArgs e)
		{
			Services.ViewManager.CleanOnExit = checkBoxDelete.Checked;
		}

		private void buttonSend_Click(object sender, EventArgs e)
		{
			_collectViewModel.SendReport();
		}
	}
}
